//
//  moreStackViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/1/13.
//  Copyright © 2021 热带雨林. All rights reserved.
//

#import "moreStackViewController.h"

@interface moreStackViewController ()
@property (nonatomic, strong) UIStackView *stackView;
@property (nonatomic, strong) UIStackView *axisStackView;
@end

@implementation moreStackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectZero];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(20);
    }];
    view1.backgroundColor = [UIColor redColor];
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectZero];
    view2.backgroundColor = [UIColor greenColor];
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectZero];
    view3.backgroundColor = [UIColor blueColor];
    UIStackView *stackView =  [[UIStackView alloc]initWithArrangedSubviews:@[view1,view2,view3]];
    [self.view addSubview:stackView];
    self.stackView = stackView;
    stackView.spacing = 5.0;
    stackView.axis = UILayoutConstraintAxisHorizontal;
    stackView.alignment = UIStackViewAlignmentFill;
    stackView.distribution = UIStackViewDistributionFillEqually;
    [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(SJHeight + 50);
        make.width.mas_equalTo(GDScreenW - 50);
        make.height.mas_equalTo(300);
    }];
    //axis
    /**
     UILayoutConstraintAxisHorizontal = 0,
        UILayoutConstraintAxisVertical  = 1
     */
    UIButton *horizontalBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [horizontalBtn setTitle:@"Horizontal" forState:UIControlStateNormal];
    [horizontalBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [horizontalBtn addTarget:self action:@selector(horizontalStyle) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *verticalBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [verticalBtn setTitle:@"Vertical" forState:UIControlStateNormal];
    [verticalBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [verticalBtn addTarget:self action:@selector(verticalStyle) forControlEvents:UIControlEventTouchUpInside];
    
    UIStackView *axisStackView = [[UIStackView alloc]initWithArrangedSubviews:@[horizontalBtn,verticalBtn]];
    self.axisStackView = axisStackView;
    axisStackView.backgroundColor = [UIColor redColor];
    [self.view addSubview:axisStackView];
    axisStackView.spacing = 5.0;
    axisStackView.axis = UILayoutConstraintAxisHorizontal;
    axisStackView.alignment = UIStackViewAlignmentFill;
    axisStackView.distribution = UIStackViewDistributionFillEqually;
    [axisStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(stackView.mas_bottom).offset(10);
        make.left.equalTo(stackView);
        make.height.mas_equalTo(75);
        make.width.mas_equalTo(GDScreenW - 50);
    }];
    //Distribution
    /**
     Distribution是设置其管理视图的排列方式，我们选择等宽充满
        typedef NS_ENUM(NSInteger, UIStackViewDistribution) {
            //充满，当只有一个控件时可以使用
            UIStackViewDistributionFill = 0,
            //平分充满，每个控件占据相同尺寸排列充满
            UIStackViewDistributionFillEqually,
            //会优先按照约束的尺寸进行排列，如果没有充满，会拉伸最后一个排列的控件充满
            UIStackViewDistributionFillProportionally,
            //等间距排列
            UIStackViewDistributionEqualSpacing,
            //中心距离相等
            UIStackViewDistributionEqualCentering,
        } NS_ENUM_AVAILABLE_IOS(9_0);
     */
    NSMutableArray *disBtnArray = [[NSMutableArray alloc]init];
    for (int i = 0; i < 5; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:[NSString stringWithFormat:@"排列方式%d",i + 1] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn addTarget:self action:@selector(clickDisBtn:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        btn.tag = i;
        [disBtnArray addObject:btn];
    }
    UIStackView *disStackView = [[UIStackView alloc]initWithArrangedSubviews:disBtnArray];
    [self.view addSubview:disStackView];
    [disStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.axisStackView.mas_bottom).offset(10);
        make.left.equalTo(self.axisStackView);
        make.width.mas_equalTo(GDScreenW / 2 - 20);
        make.height.mas_equalTo(self.stackView);
    }];
    disStackView.spacing = 3.0;
    disStackView.axis = UILayoutConstraintAxisVertical;
    disStackView.alignment = UIStackViewAlignmentFill;
    disStackView.distribution = UIStackViewDistributionFillEqually;
    //Alignment
    /**
    Alignment是选择其管理视图的对齐模式，我们这里选择充满
    typedef NS_ENUM(NSInteger, UIStackViewAlignment) {
       //水平布局时为高度充满，竖直布局时为宽度充满
        UIStackViewAlignmentFill,
        //前边对其
        UIStackViewAlignmentLeading,
        //顶部对其
        UIStackViewAlignmentTop = UIStackViewAlignmentLeading,
        //第一个控件文字的基线对其 水平布局有效
        UIStackViewAlignmentFirstBaseline,
        //中心对其
        UIStackViewAlignmentCenter,
        //后边对其
        UIStackViewAlignmentTrailing,
        //底部对其
        UIStackViewAlignmentBottom = UIStackViewAlignmentTrailing,
        //基线对其，水平布局有效
        UIStackViewAlignmentLastBaseline,
    } NS_ENUM_AVAILABLE_IOS(9_0);
    */
    NSMutableArray *aliArray = [[NSMutableArray alloc]init];
    for (int i = 0; i < 8; i ++) {
        UIButton *aliBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [aliBtn setTitle:[NSString stringWithFormat:@"对齐方式%d",i+1] forState:UIControlStateNormal];
        aliBtn.tag = i;
        [aliBtn addTarget:self action:@selector(clickAliBtn:) forControlEvents:UIControlEventTouchUpInside];
        [aliBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        aliBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [aliArray addObject:aliBtn];
    }
    UIStackView *aliStackView =  [[UIStackView alloc]initWithArrangedSubviews:aliArray];
    [self.view addSubview:aliStackView];
    [aliStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(disStackView);
        make.right.equalTo(self.stackView.mas_right);
        make.width.equalTo(disStackView);
        make.height.mas_equalTo(320);
    }];
    aliStackView.axis = UILayoutConstraintAxisVertical;
    aliStackView.spacing = 3.0;
    aliStackView.alignment = UIStackViewAlignmentFill;
    aliStackView.distribution = UIStackViewDistributionFillEqually;
}
- (void)horizontalStyle{
    self.stackView.axis = UILayoutConstraintAxisHorizontal;
//    [self.stackView layoutIfNeeded];
    [self.stackView setNeedsLayout];
}
- (void)verticalStyle{
    self.stackView.axis = UILayoutConstraintAxisVertical;
//    [self.stackView layoutIfNeeded];
    [self.stackView setNeedsLayout];
}
- (void)clickDisBtn:(UIButton *)sender{
    switch (sender.tag) {
        case 0:{
            self.stackView.distribution = UIStackViewDistributionFill;
            NSLog(@"UIStackViewDistributionFill");
            break;
        }
        case 1:{
            self.stackView.distribution = UIStackViewDistributionFillEqually;
            NSLog(@"UIStackViewDistributionFillEqually");
            break;
        }
        case 2:{
            self.stackView.distribution = UIStackViewDistributionFillProportionally;
            NSLog(@"UIStackViewDistributionFillProportionally");
            break;
        }
        case 3:{
            self.stackView.distribution = UIStackViewDistributionEqualSpacing;
            NSLog(@"UIStackViewDistributionEqualSpacing");
            break;
        }
        case 4:{
            self.stackView.distribution = UIStackViewDistributionEqualCentering;
            NSLog(@"UIStackViewDistributionEqualCentering");
            break;
        }
        default:
            break;
    }
    [self.stackView setNeedsLayout];
}
- (void)clickAliBtn:(UIButton *)sender{
    switch (sender.tag) {
        case 0:{
            self.stackView.alignment = UIStackViewAlignmentFill;
            NSLog(@"UIStackViewAlignmentFill");
            break;
        }
        case 1:{
            self.stackView.alignment = UIStackViewAlignmentLeading;
            NSLog(@"UIStackViewAlignmentLeading");
            break;
        }
        case 2:{
            self.stackView.alignment = UIStackViewAlignmentTop;
            NSLog(@"UIStackViewAlignmentTop = UIStackViewAlignmentLeading");
            break;
        }
        case 3:{
            self.stackView.alignment = UIStackViewAlignmentFirstBaseline;
            NSLog(@"UIStackViewAlignmentFirstBaseline");
            break;
        }
        case 4:{
            self.stackView.alignment = UIStackViewAlignmentCenter;
            NSLog(@"UIStackViewAlignmentCenter");
            break;
        }
        case 5:{
            self.stackView.alignment = UIStackViewAlignmentTrailing;
            NSLog(@"UIStackViewAlignmentTrailing");
            break;
        }
        case 6:{
            self.stackView.alignment = UIStackViewAlignmentBottom;
            NSLog(@"UIStackViewAlignmentBottom = UIStackViewAlignmentTrailing");
            break;
        }
        case 7:{
            self.stackView.alignment = UIStackViewAlignmentLastBaseline;
            NSLog(@"UIStackViewAlignmentLastBaseline");
            break;
        }
        default:
            break;
    }
    [self.stackView setNeedsLayout];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
